import http from '@/utils/request'

interface LoginParams {
    code: string
    username: string
    avatar: string
}

interface UserInfo {
    id: number
    username: string
    avatar: string
    email: string
    phone: string
    gender: string
    token?: string
}

// 用户账户信息接口
interface UserAccount {
    id: number
    userId: number
    points: number
    createTime: string
    updateTime: string
    balance: number
}

// 用户反馈相关接口
export interface UserFeedbackBo {
    id?: number;
    userId?: number;
    feedbackType: number; // 反馈类型(1:功能建议,2:问题投诉,3:其他)
    content: string; // 反馈内容
    contact?: string; // 联系方式
    status?: number; // 处理状态(0:未处理,1:处理中,2:已处理)
    reply?: string; // 回复内容
    image?: string; // 图片ID，多个用逗号分隔
}

export interface UserFeedbackVo {
    id: number;
    userId?: number;
    feedbackType: number;
    content: string;
    contact?: string;
    status?: number;
    reply?: string;
    image?: string;
    createTime?: string;
}

// 分页查询参数接口
interface PageQuery {
    pageNum: number;
    pageSize: number;
}

// 反馈查询参数接口
interface FeedbackQueryParams {
    userId?: number;
    feedbackType?: number;
    status?: number;
    pageQuery: PageQuery;
}

// 微信登录
export const wxLogin = (params: LoginParams) => {
    return http.post('/user/api/userInfo/code', params)
}

// 退出登录
export const logout = () => {
    // 清除本地存储
    uni.clearStorageSync()
    // 触发全局事件，通知其他页面更新状态
    uni.$emit('userLogout')
}

// 获取用户账户信息
export const getUserAccount = (userId: number) => {
    return http.get(`/user/api/account/byUserId/${userId}`)
}

// 获取用户账户列表（分页）
export const getUserAccountList = (params: any) => {
    return http.get('/user/api/account/list', { params })
}

// 更新用户账户信息
export const updateUserAccount = (data: Partial<UserAccount>) => {
    return http.put('/user/api/account', data)
}

// 更新用户信息
export const updateUserInfo = (data: Partial<UserInfo>) => {
    return http.put('/user/api/userInfo/update', data)
}

// 获取用户反馈列表
export const getFeedbackList = (params: {
    userId?: number;
    pageNum?: number;
    pageSize?: number;
    feedbackType?: number;
    status?: number;
}) => {
    // 尝试使用GET请求，直接传递参数
    return http.get('/user/api/feedback/list', {
        userId: params.userId,
        pageNum: params.pageNum || 1,
        pageSize: params.pageSize || 10,
        feedbackType: params.feedbackType,
        status: params.status
    });
};

// 获取用户反馈详情
export const getFeedbackDetail = (id: number) => {
    return http.get(`/user/api/feedback/${id}`);
};

// 新增用户反馈
export const addFeedback = (data: UserFeedbackBo) => {
    return http.post('/user/api/feedback', data);
};

// 修改用户反馈
export const updateFeedback = (data: UserFeedbackBo) => {
    return http.put('/user/api/feedback', data);
};

// 删除用户反馈
export const deleteFeedback = (ids: number[]) => {
    return http.delete(`/user/api/feedback/${ids.join(',')}`);
};

// 更新用户余额
export const updateUserBalance = (userId: number, balance: number) => {
    return http.put(`/user/api/account/updateBalance1?userId=${userId}&balance=${balance}`);
};

// 更新用户积分
export const updateUserPoints = (userId: number, points: number) => {
    return http.put(`/user/api/account/updatePoints1?userId=${userId}&points=${points}`);
}; 